import com.itheima.maper.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTset {
    @Test
    public void Tset() throws IOException {
        // 编写代码让MyBatis跑起来,执行SQL语句
        String resource = "mybatis-config.xml";
        // 加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 得到SqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 得到SqlSession(相当于数据库连接,操作数据库的对象)
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 生成UserMapper接口的实现类对象(动态代理)
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = mapper.findAllUsers();

        for (User user : users) {
            System.out.println(user);
        }

        // 释放资源
        sqlSession.close();
    }
}
